#!/usr/bin/python
import sys
import os
import pexpect

def expectandshow(child, expect):
        index=child.expect(expect)
        print child.before,child.after,
        return index

Master_IP="192.168.226.190"

cmd = "ssh root@" + Master_IP +" " + \
	"/sbin/chkconfig nfs off;" + \
	"/sbin/chkconfig nfs on;" + \
	"/sbin/chkconfig nfslock on;" + \
	"/etc/init.d/nfslock restart;" + \
	"/etc/init.d/nfs restart"

print cmd
password="passw0rd"
scp_child=pexpect.spawn(cmd)
index=expectandshow(scp_child, \
                                ['Are you sure you want to continue connecting', \
                                "root@"+Master_IP+"'s password:", \
                                pexpect.TIMEOUT, \
                                pexpect.EOF])
if index == 0:
        scp_child.sendline('yes')
        ret = expectandshow(scp_child, ["root@"+Master_IP+"'s password:", pexpect.
EOF])
        if ret == 0:
                scp_child.sendline(password)
                expectandshow(scp_child, [pexpect.EOF])
        elif ret == 1:
                pass
elif index == 1:
        scp_child.sendline(password)
        expectandshow(scp_child, [pexpect.EOF])
elif index == 2:
        print "timeout, ignore and continue."
elif index == 3:
        pass

